<?php

/**
 * 竞猜记录 @2017
 * @version 2017-10-17
 */
class GuessingLogRedisService extends BaseRedisService {

    /**
     * @var string 游戏数据前缀
     */
    public $guessing_log_db;

    /**
     * GamesRedisService constructor.
     */
    public function __construct() {
        parent::__construct();
        $this->guessing_log_db = $this->prefix . 'guessing_log:';
    }

    public function isLock() {
        $lock = $this->redis->hMGet($this->guessing_log_db . 'global', array('lock'));
        return $lock['lock'] > NOW_TIME;
    }

    public function lock($time = 5) {
        return $this->redis->hMset($this->guessing_log_db . 'global', array('lock' => NOW_TIME + $time));
    }

    public function unLock() {
        return $this->redis->hMset($this->guessing_log_db . 'global', array('lock' => 0));
    }

    public function isVideoLock($video_id) {
        $lock = $this->redis->hMGet($this->guessing_log_db . 'vedio:' . $video_id, array('lock'));
        return $lock['lock'] > NOW_TIME;
    }

    public function lockVideo($video_id, $time = 5) {
        return $this->redis->hMset($this->guessing_log_db . 'vedio:' . $video_id, array('lock' => NOW_TIME + $time));
    }

    public function unLockVideo($video_id) {
        return $this->redis->hMset($this->guessing_log_db . 'vedio:' . $video_id, array('lock' => 0));
    }

    /**
     * 添加记录
     * @param int   $guessing_log_id 游戏id
     * @param array $data
     * @return bool|int
     */
    public function insert_db($guessing_log_id, $data) {
        $guessing_log_id = intval($guessing_log_id);
        if (!$guessing_log_id) {
            return false;
        }
        filter_null($data);
        $redis = $this->redis->multi();
        $redis->hMset($this->guessing_log_db . $guessing_log_id, $data);
        $res = $redis->exec();
        if (isset($res[0]) && $res[0] !== false) {
            return $guessing_log_id;
        } else {
            return false;
        }
    }

    /*
     * 更新视频信息
     */

    public function update_db($guessing_log_id, $data, $fill = true) {
        filter_null($data);
        if (!$fill) {
            $old = $this->redis->hGetAll($this->guessing_log_db . $guessing_log_id);
            if ($old) {
                $data = array_merge($old, $data);
            }
        }
        return $this->redis->hMSet($this->guessing_log_db . $guessing_log_id, $data);
    }

    /*
     * 获取视频单个字段
     */

    public function getOne_db($guessing_log_id, $field) {
        return $this->redis->hGet($this->guessing_log_db . $guessing_log_id, $field);
    }

    /*
     * 获取多个字段
     */

    public function getRow_db($guessing_log_id, $fields = '') {
        if (!$fields) {
            return $this->redis->hGetAll($this->guessing_log_db . $guessing_log_id);
        } else {
            return $this->redis->hMGet($this->guessing_log_db . $guessing_log_id, $fields);
        }
    }

    public function del_db($guessing_log_id) {
        $guessing_log_id = intval($guessing_log_id);
        if (!$guessing_log_id) {
            return false;
        }
        return $this->redis->hDel($this->guessing_log_db . $guessing_log_id, $this->redis->hKeys($this->guessing_log_db . $guessing_log_id));
    }

}
